package pl.bank.view.beans;

import java.util.Map;

import javax.faces.application.FacesMessage;

import javax.faces.context.FacesContext;

import oracle.adf.share.logging.ADFLogger;
import oracle.adf.view.rich.component.rich.input.RichInputText;

import oracle.binding.OperationBinding;

import utils.ADFUtils;

public class ApplicationBean {
    private static final String PASSED = "passed";
    private static final String DENIDED = "denided";
    private RichInputText login;
    private RichInputText password;

    private ADFLogger LOG = ADFLogger.createADFLogger(this.getClass().getName());

    public ApplicationBean() {
    }

    public String validateData() {
        String loginInput = (String)login.getValue();
        String passwordInput = (String)password.getValue();

        OperationBinding validate = ADFUtils.findOperation("validateLoginAndPassword");
        Map map = validate.getParamsMap();
        map.put("login", loginInput);
        map.put("password", passwordInput);
        Boolean result = (Boolean)validate.execute();
        LOG.info("result " + result);
        if (result) {
            return PASSED;
        } else {
            String messageText = "Nieprawidłowy login lub hasło";
            FacesMessage fm = new FacesMessage(messageText);
            fm.setSeverity(FacesMessage.SEVERITY_WARN);
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, fm);
            return DENIDED;
        }
    }

    public void setLogin(RichInputText login) {
        this.login = login;
    }

    public RichInputText getLogin() {
        return login;
    }

    public void setPassword(RichInputText password) {
        this.password = password;
    }

    public RichInputText getPassword() {
        return password;
    }
}
